import javafx.scene.control.SplitPane;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class StudentDao {
    private static final String url = "jdbc:mysql://localhost:3306/test";
    private static final String username = "root";
    private static final String password = "9542zzzzz";

    public void insert(Student s) {
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("insert into student values(?,?)")) {
            statement.setInt(1, s.getId());
            statement.setString(2, s.getName());
            int ret = statement.executeUpdate();
            if(ret != 1) {
                throw new RuntimeException("插入学生" + s + "失败");
            }else{
                System.out.println("插入学生" + s + "成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void delete(int id) {
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("delete from student where id = ?")) {
            statement.setInt(1, id);
            int ret = statement.executeUpdate();
            if(ret != 1) {
                throw new RuntimeException("删除学生" + id + "失败");
            }else{
                System.out.println("删除学生" + id + "成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void delete(String name) {
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("delete from student where name = ?")) {
            statement.setString(1, name);
            int ret = statement.executeUpdate();
            if(ret != 1) {
                throw new RuntimeException("删除学生" + name + "失败");
            }else{
                System.out.println("删除学生" + name + "成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public List<Student> selectAll() {
        List<Student> students = new ArrayList<>();
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("select * from student");
            ResultSet resultSet = statement.executeQuery()) {
            while(resultSet.next()){
                Student s = new Student(resultSet.getInt("id"),resultSet.getString("name"));
                students.add(s);
            }
            return students;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public Student select(int id) {
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("select from student where id = ?;");
            ResultSet resultSet = statement.executeQuery()) {
            if(resultSet.next()) {
                Student s = new Student(resultSet.getInt("id"), resultSet.getString("name"));
                System.out.println("查询学生:" + s);
                return s;
            }else{
                System.out.println("查询学生为空");
                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public Student select(String name) {
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("select from student where name = ?;");
            ResultSet resultSet = statement.executeQuery()) {
            if(resultSet.next()) {
                Student s = new Student(resultSet.getInt("id"), resultSet.getString("name"));
                System.out.println("查询学生:" + s);
                return s;
            }else{
                System.out.println("查询学生为空");
                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void update(int id, String name) {
        try(Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement statement = conn.prepareStatement("update student set name = ? where id = ?")) {
            statement.setInt(2, id);
            statement.setString(1, name);
            int ret = statement.executeUpdate();
            if(ret != 1) {
                throw new RuntimeException("更新学生" + id + name + "失败");
            }else{
                System.out.println("更新学生" + id + name + "成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        StudentDao dao = new StudentDao();
        dao.insert(new Student(1,"zhangsan"));
        dao.insert(new Student(2,"lisi"));
        dao.insert(new Student(3,"wangwu"));
        dao.insert(new Student(4,"zhaoliu"));
        System.out.println(Arrays.toString(dao.selectAll().toArray()));
        dao.update(2,"wukong");
        dao.delete(3);
        System.out.println(Arrays.toString(dao.selectAll().toArray()));
    }
}
